// pages/daily_recommend/daily_recommend.js
import PubSub from 'pubsub-js';
import request from '../../utils/requests'
var today = new Date();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        day:today.getDate(),
        month:today.getMonth()+1,
        recommendList:[],
        recommendLisindex:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getrecommendsongs();

        //订阅来自playsong页面的消息
        PubSub.subscribe('switchsong',(msg , data) => {
            if(data === 'pre'){
                let index = this.data.recommendLisindex;

                if(index >=1){
                    index--;
                    let musicid = this.data.recommendList[index].id;
                    PubSub.publish('setMusicID',musicid);
                    //PubSub.unsubscribe('switchsong');
                }else{
                    wx.showToast({
                      title: '已到开头',
                      icon: 'none'
                    })
                }
                this.setData({
                    recommendLisindex:index
                })
            }
            if(data === 'next'){
                let index = this.data.recommendLisindex;

                if(index <= this.data.recommendList.length-2){
                    index++;
                    let musicid = this.data.recommendList[index].id;
                    PubSub.publish('setMusicID',musicid);
                    //PubSub.unsubscribe('switchsong');
                }
                else{
                    wx.showToast({
                      title: '已到末尾',
                      icon: 'none'
                    })
                }
                this.setData({
                    recommendLisindex:index
                })
            }
        })
    },
    async getrecommendsongs(){
        let result = await request('/recommend/songs');
        let userInfo = wx.getStorageSync('userInfo');
        if(!userInfo){
            wx.showToast({
              title: '请先登陆',
              success:() => {
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
              }
            })
        }
        if(result.code === 200){
            this.setData({
                recommendList:result.recommend
            })
        }
        //console.log(result);
    },

    toSongDetail(event){
        let song = event.currentTarget.dataset.song;
        let index = event.currentTarget.dataset.index;
        this.setData({
            recommendLisindex:index
        })
        wx.navigateTo({
          url: '/pages/playsong/playsong?musicId=' + JSON.stringify(song.id)
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})